package com.dodock.banglapapers.server.helper;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsDetailManobZamin {
	public String Generate(String newsid) throws IOException{
		//
		// Get a handle on the datastore itself
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();		
		// Lookup data by known key name
		try {
			Entity detailsEntity = datastore.get(KeyFactory.createKey("NewsDetails", "manobzamin__" + newsid));
			Text text = (Text)detailsEntity.getProperty("data");
			return text.getValue();
			
		} catch (EntityNotFoundException e) {
			
			Document doc = Jsoup.connect(Settings.WWW_MZAMIN_COM + "/details.php?nid=" + newsid).get();
			
			JsonObject news = new JsonObject();
			news.addProperty("id", newsid);
			news.addProperty("sharelink", Settings.WWW_MZAMIN_COM + "/details.php?nid=" + newsid);
			
			Element detail_news = doc.getElementById("detail_news");
			if(detail_news != null){
				Element detail_p = detail_news.getElementsByTag("p").last();
				//unwrapLinks(detail_p);
				news.addProperty("details", detail_p.html().replaceAll("&nbsp;", " "));
				
				JsonArray imageGallery = null;
				
				Element detail_news_image = detail_news.getElementById("detail_news_image1");
				if(detail_news_image != null){
					Elements imgs = detail_news_image.getElementsByTag("img");
					if(imgs != null && imgs.size() > 0){
						if(imageGallery == null){
							imageGallery = new JsonArray();
						}
						for(Element img:imgs){
							JsonObject image = new JsonObject();
							image.addProperty("url", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
							imageGallery.add(image);
						}
					}
				}
				
				detail_news_image = detail_news.getElementById("detail_news_image2");
				if(detail_news_image != null){
					Elements imgs = detail_news_image.getElementsByTag("img");
					if(imgs != null && imgs.size() > 0){
						if(imageGallery == null){
							imageGallery = new JsonArray();
						}
						for(Element img:imgs){
							JsonObject image = new JsonObject();
							image.addProperty("url", Settings.WWW_MZAMIN_COM + "/" + img.attr("src"));
							imageGallery.add(image);
						}
					}
				}
				
				if(imageGallery != null){
					news.add("gallery", imageGallery);
				}
			}
			
			
			String data = news.toString();
			Text text = new Text(data);
			/* save here */
			Entity detailsEntity = new Entity("NewsDetails", "manobzamin__" + newsid);
			detailsEntity.setProperty("data", text);
			datastore.put(detailsEntity);
			return data;
		}
	}
	
	private void unwrapLinks(Element parent){
		if(parent != null){
			Elements as = parent.getElementsByTag("a");
			if(as != null){
				as.unwrap();
			}
		}
	}
	
	private void removeTeasers(Element parent){
		if(parent != null){
			Elements as = parent.select("div[class^=imgTeaserM]");
			if(as != null){
				as.remove();
			}
		}
	}
}
